package com.wlw.web;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wlw.entity.City;
import com.wlw.entity.Foreign;
import com.wlw.entity.Gonglue;
import com.wlw.entity.Province;
import com.wlw.entity.TravalNote;
import com.wlw.entity.User;
import com.wlw.service.CityService;
import com.wlw.service.ForeignService;
import com.wlw.service.GonglueService;
import com.wlw.service.ProvinceService;
import com.wlw.service.TravalNoteService;

import edu.xjsoft.base.orm.support.Page;
import edu.xjsoft.base.orm.support.PropertyFilter;

@Controller
@RequestMapping("/travalNoteManage/*")
public class TravalNoteAction {

	@Autowired
	private TravalNoteService travalNoteService;
	@Autowired
	private ProvinceService provinceService;
	@Autowired
	private ForeignService foreignService;
	@Autowired
	private GonglueService gonglueService;
	@Autowired
	private CityService cityService;
	//进入前台旅游攻略界面：
	@RequestMapping("traval")
	public String traval(Model model){
		/**
		 * 攻略导航
		 */
		System.out.println("旅游攻略！");
		List<Province> xiProvinces = provinceService.xiBuProvinces();
		model.addAttribute("xiProvinces", xiProvinces);
		List<Province> zhongProvinces = provinceService.zhongBuProvinces();
		model.addAttribute("zhongProvinces", zhongProvinces);
		List<Province> dongProvinces = provinceService.DongBuProvinces();
		model.addAttribute("dongProvinces", dongProvinces);
		List<Foreign> allForeigns = foreignService.Allforeigns();
		model.addAttribute("allForeigns", allForeigns);
		/**
		 * 攻略排行
		 */
		List<Gonglue> hotGonglues3 = gonglueService.hotGonglues3();
		model.addAttribute("hotGonglues3", hotGonglues3);
		/**
		 * 最新更新
		 */
		List<Gonglue> newGonglues = gonglueService.newGonglues();
		model.addAttribute("newGonglues", newGonglues);
		return "foreground/traval";
	}
	//发表游记模块：
	@RequestMapping("publish")
	public String publish(HttpServletRequest req,Model model){
		String title = req.getParameter("title");
		String content = req.getParameter("content");
		String city = req.getParameter("city");
		User user = (User) req.getSession().getAttribute("user");
		if (user!=null&&city!=null&&title!=null&&content!=null) {
			PropertyFilter filters = new PropertyFilter("name", city);
			List<City> cities = cityService.search(filters);
			if (cities.size()!=0) {
				Date time = new Date();
				TravalNote travalNote = new TravalNote();
				travalNote.setCity(cities.get(0));
				travalNote.setTitle(title);
				travalNote.setContent(content);
				travalNote.setTime(time.toLocaleString());
				travalNoteService.saveOrUpdate(travalNote);
			}
		}
		
		//显示全部游记
		List<TravalNote> listTravalNotes = travalNoteService.loadAll();
		model.addAttribute("listTravalNotes", listTravalNotes);
		return "foreground/ListTravalNote";
	}
	//显示游记列表：listTravalNotes
	@RequestMapping("listTravalNote")
	public String listTravalNote(Model model){
		List<TravalNote> listTravalNotes = travalNoteService.loadAll();
		model.addAttribute("listTravalNotes", listTravalNotes);
		return "foreground/ListTravalNote";
	}
//后台代码：
////////////////////////////////////////
	@RequestMapping("{pageNo}")
	public String list(@PathVariable int pageNo,Model model){
		System.out.println("travalNoteManage!");
		Page<TravalNote> page = new Page<TravalNote>(2);
		page.setPageNo(pageNo);
		travalNoteService.loadAll(page);
		model.addAttribute("page", page);
		return "background/travalNoteManage";
	}
	@RequestMapping("delTra")
	public String delTra(HttpServletRequest req){
		int id = Integer.parseInt(req.getParameter("id"));
		travalNoteService.remove(id);
		return "background/successDel";
	}
	@RequestMapping("multiDel")
	public String multiDel(HttpServletRequest req){
		String name[] = req.getParameterValues("checkbox");
		for (String i : name) {
			int id = Integer.parseInt(i);
			travalNoteService.remove(id);
		}
		return "background/successDel";
	}
////////////////////////////////////////////////////////////
}
